<template>
  <div>
    <input type="text" v-if="flag" ref="inputFocus" />
    <button @click="search" v-else>点击搜索</button>
  </div>
</template>

<script>
// dom.focus() 获取焦点
export default {
  data() {
    return {
      flag: false,
    }
  },
  methods: {
    search() {
      this.flag = !this.flag
      this.$nextTick(() => {
        this.$refs.inputFocus.focus()
        console.log(this.$refs.inputFocus)
      })
    },
  },
}
</script>

<style></style>
